package com.kidedu.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;


import net.sf.json.JSONObject;

import com.kidedu.entity.KGrade;
import com.kidedu.entity.KStudent;
import com.kidedu.service.IKGradeService;
import com.kidedu.service.IKOrderService;
import com.kidedu.service.IKStudentService;
import com.kidedu.util.RespJSON;
import com.kidedu.util.SpringContextUtil;
import com.kidedu.vo.StudentVoGrade;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class KStudentAction {

	private String username;
	private String password;
	private String studentIdList;
	private int pageSize;
	private int pageNum;
	private KStudent student;
	private JSONObject json=new JSONObject();
    private 	 IKStudentService  kStudentService= (IKStudentService) SpringContextUtil.getBean("iKStudentService");
    private   IKOrderService  kOrderService=(IKOrderService)SpringContextUtil.getBean("iKOrderService");
    private   IKGradeService  kGradeService=(IKGradeService)SpringContextUtil.getBean("iKGradeService");
	public String login()
	{
		   if(kStudentService.Login(username, password))		
		   {
			   ActionContext.getContext().getSession().put("ctx", ServletActionContext.getServletContext().getContextPath());
			   
			   ActionContext.getContext().getSession().put("load","yes");
			   
			   ActionContext.getContext().getSession().put("username", username);
			   return Action.SUCCESS;	 
		   }
		   return "fail";
	}
	public String StudentLoginOut(){
	    HttpServletRequest req = ServletActionContext.getRequest();
	    req.getSession().removeAttribute("STUDENT");
		return Action.SUCCESS;
	}
	public String AddSingleStudent()
	{
		RespJSON respJSON = new RespJSON();
	
		if(kStudentService.AddSingleStudent(student))
		{
			respJSON.setStatusCode(200);
			respJSON.setStatusText("OK");
		}
			
		else
		{
			respJSON.setStatusCode(500);
			respJSON.setStatusText("NO");
		}
		json = JSONObject.fromObject(respJSON);
		return Action.SUCCESS;
	}
	
	public String DeleteStudent()
	{
		kStudentService.deleteSingleStudent(studentIdList);
		return Action.SUCCESS;
	}
	
public String StudentList()
{
	//RespJSON respJSON = new RespJSON();
	
	json=kStudentService.StudentList(pageNum, pageSize);
	return Action.SUCCESS;
}

public String Stu_info()
{
   StudentVoGrade SVG=new StudentVoGrade();
   HttpServletRequest req = ServletActionContext.getRequest();
   KStudent student=(KStudent)req.getSession().getAttribute("STUDENT");
   SVG.setStudent(student);
   List<String> GidList=kOrderService.selectOrderByStuIdReturnGid(student.getSid());
   List<KGrade> gradeList=kGradeService.selectMysGrade(GidList);
   SVG.setGradeList(gradeList);
	RespJSON respJSON = new RespJSON();
	respJSON.setStatusCode(200);
	respJSON.setStatusText("OK");
	respJSON.setData(SVG);
	json = JSONObject.fromObject(respJSON);
   	return Action.SUCCESS;
}

public String updateSingleStudent()
{
	
	 String[] info=studentIdList.split(",");
	 student=new KStudent();
	 student.setSid(info[0]);
	 student.setSparentname(info[1]);
	 student.setSparenttel(info[2]);
	kStudentService.updateSingleStudent(student);
	return Action.SUCCESS;
}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public JSONObject getJson() {
		return json;
	}
	public void setJson(JSONObject json) {
		this.json = json;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	public String getStudentIdList() {
		return studentIdList;
	}
	public void setStudentIdList(String studentIdList) {
		this.studentIdList = studentIdList;
	}
	
}
